package com.local.admin.controller;

import com.local.admin.constants.AdminConstants;
import com.local.admin.constants.IndexConfigTypeEnum;
import com.local.common.constants.BusinessException;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.ResultUtil;
import com.local.indexconfig.feign.IndexConfigServiceFeign;
import com.local.indexconfig.pojo.IndexConfig;
import com.local.indexconfig.pojo.IndexConfigDeleteRequestParam;
import com.local.indexconfig.pojo.IndexConfigListRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/admin/indexconfig")
public class AdminIndexConfigController {

    @Autowired
    private IndexConfigServiceFeign indexConfigServiceFeign;

    /**
     * 商品配置页面
     * @param configType
     * @param request
     * @return
     */
    @GetMapping
    public String index(@RequestParam("configType") Integer configType, HttpServletRequest request) {
        IndexConfigTypeEnum indexConfigTypeEnum = IndexConfigTypeEnum.getIndexConfigTypeEnumByType(configType);
        if (indexConfigTypeEnum.equals(IndexConfigTypeEnum.DEFAULT)) {
            throw new BusinessException(ExceptionCodeEnum.BUSINESS_EXCEPTION.getCode(), "参数错误");
        }

        request.setAttribute("path", indexConfigTypeEnum.getName());
        request.setAttribute("configType", configType);

        return "admin/index_config";
    }


    /**
     * 获取配置商品列表，支持翻页
     * @param params Map<String, Object>: configType: 3|4|5, limit: 10, page: 1, order: asc, sidx: (排序字段，默认为空)
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam Map<String, Object> params) {
        if (Objects.isNull(params.get("configType"))) {
            return new ResultUtil(201, "参数错误");
        }

        Integer configType = Integer.valueOf(params.get("configType").toString());
        Integer page = Objects.nonNull(params.get("page")) ? Integer.valueOf(params.get("page").toString()) : 1;
        Integer limit = Objects.nonNull(params.get("limit")) ? Integer.valueOf(params.get("limit").toString()) : 10;
        String sort = Objects.nonNull(params.get("sidx")) ? (params.get("sidx").toString().length() > 0 ? params.get("sidx").toString() : "config_rank") : "config_rank";
        String order = Objects.nonNull(params.get("order")) ? (params.get("order").toString().length() > 0 ? params.get("order").toString() : "asc") : "asc";
        Integer isDeleted = -1;
        IndexConfigListRequestParam requestParam = new IndexConfigListRequestParam(
                configType, page, limit, sort, order, isDeleted);

        ResultUtil result = indexConfigServiceFeign.list(requestParam);
        return result;
    }


    /**
     * 新增
     * @param indexConfig
     * @param session
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody IndexConfig indexConfig, HttpSession session) {
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        indexConfig.setCreateUser(loginUserId);
        indexConfig.setUpdateUser(loginUserId);
        indexConfig.setCreateTime(dateTime);
        indexConfig.setUpdateTime(dateTime);

        ResultUtil saveResult = indexConfigServiceFeign.save(indexConfig);
        return saveResult;
    }


    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody IndexConfig indexConfig, HttpSession session) {
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        indexConfig.setUpdateUser(loginUserId);
        indexConfig.setUpdateTime(dateTime);

        ResultUtil updateResult = indexConfigServiceFeign.update(indexConfig);
        return updateResult;
    }


    /**
     * 根据id删除，支持批量删除
     * @param ids
     * @param session
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(@RequestBody Integer[] ids, HttpSession session) {
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());

        IndexConfigDeleteRequestParam requestParam = new IndexConfigDeleteRequestParam(
                ids, dateTime, loginUserId);
        ResultUtil deleteResult = indexConfigServiceFeign.delete(requestParam);
        return deleteResult;
    }
}
